package com.csq.cms.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.csq.cms.mapper.ParttimeInfoMapper;
import com.csq.cms.pojo.SeachParttime;
import com.csq.cms.service.ParttimeService;
import com.csq.common.pojo.ReturnResult;
import com.csq.common.pojo.StatusCode;
import com.csq.manager.mapper.ParttimeMapper;
import com.csq.manager.pojo.Parttime;
import com.csq.manager.pojo.ParttimeExample;
import com.csq.manager.pojo.ParttimeExample.Criteria;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class ParttimeServiceImpl implements ParttimeService {

	@Autowired
	private ParttimeMapper parttimeMapper;
	@Autowired
	private ParttimeInfoMapper parttimeInfoMapper;

	@Override
	public ReturnResult addParttime(Parttime parttime) {
		// TODO Auto-generated method stub

		int insertSelective = parttimeMapper.insertSelective(parttime);

		if (insertSelective == 0) {
			return ReturnResult.build(StatusCode.INSERTFAILL, "插入失败");
		}

		return ReturnResult.ok();
	}

	@Override
	public PageInfo<Parttime> getAllNewParttime(Integer page, Integer row) {
		// TODO Auto-generated method stub

		if (page == null) {
			page = 1;
		}
		if (row == null) {
			row = 10;
		}

		// 分页设置
		PageHelper.startPage(page, row);
		// 执行查询
		ParttimeExample example = new ParttimeExample();
		example.setOrderByClause("publish_date desc");

		// 有效的兼职
		Criteria createCriteria = example.createCriteria();
		createCriteria.andStateEqualTo((short) 1);

		List<Parttime> list = parttimeMapper.selectByExample(example);
		// 取分页信息
		PageInfo<Parttime> pageInfo = new PageInfo<>(list);
		System.out.println("总记录数：" + pageInfo.getTotal());
		System.out.println("总页数：" + pageInfo.getPages());
		System.out.println(list.size());

		return pageInfo;
	}

	@Override
	public PageInfo<Parttime> getAllHotParttime(Integer page, Integer row) {
		// TODO Auto-generated method stub
		if (page == null) {
			page = 1;
		}
		if (row == null) {
			row = 10;
		}

		// 分页设置
		PageHelper.startPage(page, row);
		// 执行查询
		ParttimeExample example = new ParttimeExample();

		example.setOrderByClause("signup_num desc");

		// 有效的兼职
		Criteria createCriteria = example.createCriteria();
		createCriteria.andStateEqualTo((short) 1);

		List<Parttime> list = parttimeMapper.selectByExample(example);
		// 取分页信息
		PageInfo<Parttime> pageInfo = new PageInfo<>(list);
		System.out.println("总记录数：" + pageInfo.getTotal());
		System.out.println("总页数：" + pageInfo.getPages());
		System.out.println(list.size());

		return pageInfo;
	}

	@Override
	public ReturnResult updateParttime(Parttime parttime) {
		int updateByPrimaryKeySelective = parttimeMapper
				.updateByPrimaryKeySelective(parttime);

		if (updateByPrimaryKeySelective == 0) {
			return ReturnResult.build(StatusCode.UPDATEFAIL, "更新失败！！");
		}
		return ReturnResult.ok();
	}

	@Override
	public PageInfo<SeachParttime> getSeachParttime(String keywords,
			Integer page, Integer row) {
		// TODO Auto-generated method stub
		if (page == null) {
			page = 1;
		}
		if (row == null) {
			row = 10;
		}

		System.out.println(keywords);

		if (keywords == null) {
			return null;
		}

		// 分页设置
		PageHelper.startPage(page, row);
		List<SeachParttime> list = parttimeInfoMapper
				.getSeachParttime(keywords);

		// for (SeachParttime seachParttime : list) {
		// System.out.println(seachParttime);
		// }

		// 取分页信息
		PageInfo<SeachParttime> pageInfo = new PageInfo<>(list);
		System.out.println("总记录数：" + pageInfo.getTotal());
		System.out.println("总页数：" + pageInfo.getPages());
		System.out.println(list.size());

		return pageInfo;
	}

	@Override
	public Parttime getParttimeById(Integer parttimeId) {
		return parttimeMapper.selectByPrimaryKey(parttimeId);
	}

	@Override
	public PageInfo<Parttime> getParttimeByEnterpriseIdAndType(
			Integer enterpriseId, Integer type, Integer page, Integer row) {
		if (page == null) {
			page = 1;
		}
		if (row == null) {
			row = 10;
		}

		PageHelper.startPage(page, row);

		ParttimeExample example = new ParttimeExample();

		Criteria createCriteria = example.createCriteria();
		createCriteria.andEnterpriseIdEqualTo(enterpriseId);

		if (type != 2) {
			int t = type;
			createCriteria.andStateEqualTo((short) t);
		}
		List<Parttime> list = parttimeMapper.selectByExample(example);

		// for (SeachParttime seachParttime : list) {
		// System.out.println(seachParttime);
		// }

		// 取分页信息
		PageInfo<Parttime> pageInfo = new PageInfo<>(list);
		System.out.println("总记录数：" + pageInfo.getTotal());
		System.out.println("总页数：" + pageInfo.getPages());
		System.out.println(list.size());

		return pageInfo;
	}

}
